﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Ftp.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Model.Helper
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using AlexPilotti.FTPS.Client;
    using AlexPilotti.FTPS.Common;

    using Dhgms.DataManager.Model.Helper;

    /// <summary>
    /// Helper methods for the Ftp library
    /// </summary>
    public static class Ftp
    {
        #region Public Methods and Operators

        /// <summary>
        /// Checks if a subdirectory exists
        /// </summary>
        /// <param name="ftpsClient">
        /// The FTP Client Class
        /// </param>
        /// <param name="currentDir">
        /// The current directory to search
        /// </param>
        /// <param name="wantedSubDirectory">
        /// The subdirectory we want to make sure exists
        /// </param>
        /// <returns>
        /// true if exists, otherwise false
        /// </returns>
        public static bool GetDirectoryExists(this FTPSClient ftpsClient, string currentDir, string wantedSubDirectory)
        {
            if (ftpsClient == null)
            {
                throw new ArgumentNullException("ftpsClient");
            }

            if (currentDir == null || currentDir.Trim().Length == 0)
            {
                throw new ArgumentNullException("currentDir");
            }

            if (wantedSubDirectory == null || wantedSubDirectory.Trim().Length == 0)
            {
                throw new ArgumentNullException("wantedSubDirectory");
            }

            IList<DirectoryListItem> subDirs = ftpsClient.GetDirectoryList(currentDir);
            return subDirs.Any(dir => dir.Name.Equals(wantedSubDirectory));
        }

        /// <summary>
        /// Sets the current directory on the FTP Session
        /// </summary>
        /// <param name="ftpsClient">
        /// The FTP Client Class
        /// </param>
        /// <param name="remoteDirName">
        /// The directory to change to
        /// </param>
        /// <param name="ensureExists">
        /// Whether to make sure the directory exists
        /// </param>
        public static void SetCurrentDirectory(this FTPSClient ftpsClient, string remoteDirName, bool ensureExists)
        {
            if (ftpsClient == null)
            {
                throw new ArgumentNullException("ftpsClient");
            }

            if (remoteDirName == null || remoteDirName.Trim().Length == 0)
            {
                throw new ArgumentNullException("remoteDirName");
            }

            if (!remoteDirName.StartsWith("/"))
            {
                remoteDirName = "/" + remoteDirName;
            }

            if (!remoteDirName.EndsWith("/"))
            {
                remoteDirName = remoteDirName + "/";
            }

            if (ensureExists)
            {
                // the reason this is done like this is because the checks to ensure the / is at either end
                // will add it if it is MISSING, if it's already passed in, it needs to be dealt with anyway
                var toSplit = remoteDirName.Right(remoteDirName.Length - 1);
                toSplit = toSplit.Left(toSplit.Length - 1);
                var dirs = toSplit.Split('/');

                var currentDir = "/";
                foreach (var dir in dirs)
                {
                    if (!ftpsClient.GetDirectoryExists(currentDir, dir))
                    {
                        ftpsClient.MakeDir(currentDir + dir);
                    }

                    currentDir = currentDir + dir + "/";
                }
            }

            ftpsClient.SetCurrentDirectory(remoteDirName);
        }

        #endregion
    }
}